package com.sd.trans.service.impl;

import com.sd.trans.entity.TransRec;
import com.sd.trans.dao.TransRecDao;
import com.sd.trans.service.TransRecService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;

/**
 * (TransRec)表服务实现类
 *
 * @author teacherChen
 * @since 2025-11-04 11:12:37
 */
@Service("transRecService")
public class TransRecServiceImpl implements TransRecService {
    @Resource
    private TransRecDao transRecDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TransRec queryById(Integer id) {
        return this.transRecDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param transRec 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<TransRec> queryByPage(TransRec transRec, PageRequest pageRequest) {
        long total = this.transRecDao.count(transRec);
        return new PageImpl<>(this.transRecDao.queryAllByLimit(transRec, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param transRec 实例对象
     * @return 实例对象
     */
    @Override
    public TransRec insert(TransRec transRec) {
        //将系统时间赋给transDate
        transRec.setTransDate(new Date());
        this.transRecDao.insert(transRec);
        return transRec;
    }

    /**
     * 修改数据
     *
     * @param transRec 实例对象
     * @return 实例对象
     */
    @Override
    public TransRec update(TransRec transRec) {
        //将系统时间赋给transDate
        transRec.setTransDate(new Date());
        this.transRecDao.update(transRec);
        return this.queryById(transRec.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.transRecDao.deleteById(id) > 0;
    }

    @Override
    public TransRec queryByFromTextAndLang(String fromText, String lang) {
        return this.transRecDao.queryByFromTextAndLang(fromText, lang);
    }
}
